Manon.icu

I'm here to make you a better developer by teaching you everything I know about building for the web.

Published 2022-05-23

Next.js - Response Helpers

Next.js 的响应有着类似于 express.js 的方法,方便开发

  • res.status(code):设置响应状态码
  • res.json(data):设置响应数据,并设置响应类型为 json
  • res.send(body):设置响应数据,响应类型可以是stringobjectbuffer

创建/api/user接口

export default (req, res) => {
  res.status(200).json({
    message: 'Hello World'
  })
}

访问/api/user接口

npm run dev

curl http://localhost:3000/api/user

#{message:'Hello World'}

Comments

No Comments!